package com.itheima.task.controller.v1;

import com.itheima.model.audit.pojos.AuditTask;
import com.itheima.model.common.dtos.ResponseResult;
import com.itheima.task.service.AuditService;
import com.itheima.task.service.DelayQueueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 审核管理接口
 */
@RestController
@RequestMapping("/api/v1/audit")
@Api(tags = "审核管理")
public class AuditController {

    @Autowired
    private DelayQueueService delayQueueService;

    @Autowired
    private AuditService auditService;

    /**
     * 提交审核任务
     */
    @ApiOperation("提交审核任务")
    @PostMapping("/submit")
    public ResponseResult submitAudit(@RequestBody AuditTask task) {
        // 默认延迟0秒，立即执行
        return delayQueueService.addDelayTask(task, 0);
    }

    /**
     * 查询任务状态
     */
    @ApiOperation("查询任务状态")
    @GetMapping("/status/{taskId}")
    public ResponseResult getTaskStatus(
            @ApiParam("任务ID") @PathVariable String taskId) {
        return delayQueueService.getTaskStatus(taskId);
    }

    /**
     * 取消任务
     */
    @ApiOperation("取消任务")
    @DeleteMapping("/cancel/{taskId}")
    public ResponseResult cancelTask(
            @ApiParam("任务ID") @PathVariable String taskId) {
        return delayQueueService.cancelTask(taskId);
    }

    /**
     * 人工审核通过
     */
    @ApiOperation("人工审核通过")
    @PostMapping("/manual/pass")
    public ResponseResult manualPass(
            @ApiParam("任务ID") @RequestParam String taskId,
            @ApiParam("审核员ID") @RequestParam Integer auditorId,
            @ApiParam("审核意见") @RequestParam(required = false) String comment) {
        return auditService.manualPass(taskId, auditorId, comment);
    }

    /**
     * 人工审核拒绝
     */
    @ApiOperation("人工审核拒绝")
    @PostMapping("/manual/reject")
    public ResponseResult manualReject(
            @ApiParam("任务ID") @RequestParam String taskId,
            @ApiParam("审核员ID") @RequestParam Integer auditorId,
            @ApiParam("审核意见") @RequestParam String comment) {
        return auditService.manualReject(taskId, auditorId, comment);
    }

    /**
     * 获取待人工审核列表
     */
    @ApiOperation("获取待人工审核列表")
    @GetMapping("/manual/pending")
    public ResponseResult getPendingTasks(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam("每页数量") @RequestParam(defaultValue = "20") Integer size) {
        return auditService.getPendingManualAuditTasks(page, size);
    }
}

